Skip to content

Conversation

@everettbu
Copy link
Contributor

@everettbu everettbu commented Jul 26, 2025

Test 6

Summary by CodeRabbit

  • New Features

    • The user profile now displays a more accurate website name, adapting its format based on the relationship between the user's website and the forum's domain.
  • Bug Fixes

    • Improved logic for extracting and displaying the website name in user profiles.
  • Documentation

    • Corrected documentation for user profile properties.
  • Tests

    • Enhanced test coverage for website name display scenarios in user profiles.

@coderabbitai
Copy link

coderabbitai bot commented Jul 26, 2025

Walkthrough

The changes remove the websiteName computed property from the user controller, shift the responsibility for generating the website name to the backend serializer, and update the user profile template to use the new model.website_name property. Documentation and tests are updated to reflect these changes, ensuring correct serialization and display of the website name.

Changes

Cohort / File(s) Change Summary
User Controller Logic Removal
app/assets/javascripts/discourse/controllers/user.js.es6
Removed the websiteName computed property, eliminating frontend logic for extracting the website name from the user's website URL.
User Model Documentation Fix
app/assets/javascripts/discourse/models/user.js.es6
Corrected a JSDoc comment, changing the property tag from websiteName to profileBackground. No code logic was modified.
User Profile Template Update
app/assets/javascripts/discourse/templates/user/user.hbs
Updated template to reference model.website_name instead of websiteName for displaying the website name, reflecting the backend-driven approach.
User Serializer Enhancement
app/serializers/user_serializer.rb
Added website_name as a serialized attribute, with logic to extract and format the website name based on the relationship between the website and instance hostnames. Handles edge cases and URI errors. Includes conditional inclusion logic for the attribute.
User Serializer Tests
spec/serializers/user_serializer_spec.rb
Modified existing tests to use a new website URL and added a context with tests for the website_name attribute, verifying correct serialization under various domain relationships between the instance and website.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server (Serializer)
    participant Database

    Client->>Server (Serializer): Request user profile data
    Server (Serializer)->>Database: Fetch user record
    Database-->>Server (Serializer): Return user data
    Server (Serializer)->>Server (Serializer): Compute website_name from website URL and instance hostname
    Server (Serializer)-->>Client: Return user profile JSON (includes website_name)
    Client->>Client: Render user profile with model.website_name
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~15 minutes

Poem

A bunny hops through fields of code,
Where website names once frontend showed.
Now backend wisdom takes the lead,
With serializers swift indeed.
Templates updated, tests in line—
This patch is crisp, the logic fine!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch url-handling-post

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@everettbu
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Jul 29, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7a5fbae and 267d8be.

📒 Files selected for processing (5)
  • app/assets/javascripts/discourse/controllers/user.js.es6 (0 hunks)
  • app/assets/javascripts/discourse/models/user.js.es6 (1 hunks)
  • app/assets/javascripts/discourse/templates/user/user.hbs (1 hunks)
  • app/serializers/user_serializer.rb (2 hunks)
  • spec/serializers/user_serializer_spec.rb (1 hunks)
💤 Files with no reviewable changes (1)
  • app/assets/javascripts/discourse/controllers/user.js.es6
🧰 Additional context used
🧬 Code Graph Analysis (1)
spec/serializers/user_serializer_spec.rb (1)
app/serializers/user_serializer.rb (1)
  • website (133-135)
🪛 RuboCop (1.76.1)
app/serializers/user_serializer.rb

[convention] 137-151: Assignment Branch Condition size for website_name is too high. [<2, 35, 12> 37.05/23]

(Metrics/AbcSize)


[convention] 137-151: Cyclomatic complexity for website_name is too high. [8/7]

(Metrics/CyclomaticComplexity)


[convention] 137-151: Perceived complexity for website_name is too high. [9/8]

(Metrics/PerceivedComplexity)


[convention] 138-138: Avoid using rescue in its modifier form.

(Style/RescueModifier)

🔇 Additional comments (8)
app/serializers/user_serializer.rb (2)

43-43: LGTM! Clean attribute addition.

The website_name attribute is properly added to the serializer attributes list.


153-155: LGTM! Proper conditional inclusion.

The include_website_name method correctly follows the established pattern for conditional attribute inclusion, ensuring website_name is only serialized when a website is present.

app/assets/javascripts/discourse/models/user.js.es6 (1)

59-59: LGTM! Documentation fix.

The JSDoc property tag is correctly updated to match the actual property name profileBackground.

app/assets/javascripts/discourse/templates/user/user.hbs (3)

66-66: LGTM! Correct template update.

The conditional check is properly updated to use the new serialized attribute model.website_name.


69-69: LGTM! Consistent attribute usage.

The link text correctly uses model.website_name while maintaining the proper href attribute with model.website.


71-71: LGTM! Complete template consistency.

The span content is properly updated to use model.website_name for display while keeping the title attribute as model.website.

spec/serializers/user_serializer_spec.rb (2)

69-74: LGTM! Enhanced test setup.

The website URL is updated to include a path component, providing more comprehensive test coverage for the website_name functionality.


76-91: Excellent test coverage for website_name logic.

The test cases properly cover all three domain matching scenarios implemented in the serializer:

  1. Different domains → returns host only
  2. Same domains → returns host + path
  3. Subdomain relationship → returns host + path

The use of Discourse.stubs(:current_hostname) correctly simulates different hostname scenarios.

Comment on lines +137 to +151
def website_name
website_host = URI(website.to_s).host rescue nil
discourse_host = Discourse.current_hostname
return if website_host.nil?
if website_host == discourse_host
# example.com == example.com
website_host + URI(website.to_s).path
elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2
# www.example.com == forum.example.com
website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host
else
# example.com == forum.example.com
discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host
end
end
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refactor to reduce complexity and improve readability.

The method has high cyclomatic complexity (8/7) and contains several style issues. Consider breaking this down into smaller, more focused methods.

def website_name
-  website_host = URI(website.to_s).host rescue nil
-  discourse_host = Discourse.current_hostname
-  return if website_host.nil?
-  if website_host == discourse_host
-    # example.com == example.com
-    website_host + URI(website.to_s).path
-  elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2
-    # www.example.com == forum.example.com
-    website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host
-  else
-    # example.com == forum.example.com
-    discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host
-  end
+  return unless website_host = extract_website_host
+  
+  discourse_host = Discourse.current_hostname
+  
+  if should_show_full_path?(website_host, discourse_host)
+    website_host + website_path
+  else
+    website_host
+  end
+end
+
+private
+
+def extract_website_host
+  begin
+    URI(website.to_s).host
+  rescue URI::InvalidURIError
+    nil
+  end
+end
+
+def website_path
+  begin
+    URI(website.to_s).path
+  rescue URI::InvalidURIError
+    ""
+  end
+end
+
+def should_show_full_path?(website_host, discourse_host)
+  exact_domain_match?(website_host, discourse_host) ||
+    subdomain_of_same_domain?(website_host, discourse_host) ||
+    parent_domain_match?(website_host, discourse_host)
+end
+
+def exact_domain_match?(website_host, discourse_host)
+  website_host == discourse_host
+end
+
+def subdomain_of_same_domain?(website_host, discourse_host)
+  return false unless both_have_subdomains?(website_host, discourse_host)
+  
+  website_parts = website_host.split('.')
+  discourse_parts = discourse_host.split('.')
+  
+  website_parts[1..-1] == discourse_parts[1..-1]
+end
+
+def parent_domain_match?(website_host, discourse_host)
+  discourse_host.ends_with?(".#{website_host}")
+end
+
+def both_have_subdomains?(website_host, discourse_host)
+  website_host.split('.').length == discourse_host.split('.').length &&
+    discourse_host.split('.').length > 2
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def website_name
website_host = URI(website.to_s).host rescue nil
discourse_host = Discourse.current_hostname
return if website_host.nil?
if website_host == discourse_host
# example.com == example.com
website_host + URI(website.to_s).path
elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2
# www.example.com == forum.example.com
website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host
else
# example.com == forum.example.com
discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host
end
end
def website_name
return unless website_host = extract_website_host
discourse_host = Discourse.current_hostname
if should_show_full_path?(website_host, discourse_host)
website_host + website_path
else
website_host
end
end
private
def extract_website_host
begin
URI(website.to_s).host
rescue URI::InvalidURIError
nil
end
end
def website_path
begin
URI(website.to_s).path
rescue URI::InvalidURIError
""
end
end
def should_show_full_path?(website_host, discourse_host)
exact_domain_match?(website_host, discourse_host) ||
subdomain_of_same_domain?(website_host, discourse_host) ||
parent_domain_match?(website_host, discourse_host)
end
def exact_domain_match?(website_host, discourse_host)
website_host == discourse_host
end
def subdomain_of_same_domain?(website_host, discourse_host)
return false unless both_have_subdomains?(website_host, discourse_host)
website_parts = website_host.split('.')
discourse_parts = discourse_host.split('.')
website_parts[1..-1] == discourse_parts[1..-1]
end
def parent_domain_match?(website_host, discourse_host)
discourse_host.ends_with?(".#{website_host}")
end
def both_have_subdomains?(website_host, discourse_host)
website_host.split('.').length == discourse_host.split('.').length &&
discourse_host.split('.').length > 2
end
🧰 Tools
🪛 RuboCop (1.76.1)

[convention] 137-151: Assignment Branch Condition size for website_name is too high. [<2, 35, 12> 37.05/23]

(Metrics/AbcSize)


[convention] 137-151: Cyclomatic complexity for website_name is too high. [8/7]

(Metrics/CyclomaticComplexity)


[convention] 137-151: Perceived complexity for website_name is too high. [9/8]

(Metrics/PerceivedComplexity)


[convention] 138-138: Avoid using rescue in its modifier form.

(Style/RescueModifier)

🤖 Prompt for AI Agents
In app/serializers/user_serializer.rb around lines 137 to 151, the website_name
method is too complex and hard to read due to multiple nested conditionals and
repeated URI parsing. Refactor by extracting URI parsing into a separate method,
and split the conditional logic into smaller helper methods that each handle one
comparison case. This will reduce cyclomatic complexity and improve readability
by making each method focused and easier to understand.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants